import { defineStore } from 'pinia'
import { CrudSchema } from '@/hooks/web/useCrudSchemas'

export interface SelectedItem {
  id: number
  title: string
}

export interface selectionRow {
  id: number
  product_name: string
  unit: string
  num: number
  unit_price: number
  unit_sum: number
  sales_price: number
  notes: string
  warehouse_name: string
  qty: number
  is_futures_value: string
  actual_price: number
  productid: number
  is_futures: number
  sku: string
}

// 定义一个函数来创建store的ID
function createTableDialogStoreId(id) {
  return `tableDialog-${id}`
}

export const useTableDialogStore = (id) =>
  defineStore(createTableDialogStoreId(id), {
    state: () => ({
      isTableDialogVisible: false,
      schema: <CrudSchema>[],
      selectedItems: [] as SelectedItem[],
      limit: 2,
      selectionRow: [] as selectionRow[]
    }),
    actions: {
      openDialog() {
        this.isTableDialogVisible = true
      },
      closeDialog() {
        this.isTableDialogVisible = false
      },
      setSchema(newSchema) {
        this.schema = newSchema
      },
      removeItem(itemId) {
        this.selectedItems = this.selectedItems.filter((item: SelectedItem) => item.id !== itemId)
      },
      reset() {
        this.$reset()
      },
      dialogSubmit() {}
    }
  })()
